<template>
  <div class="page-container" v-if="langChange">
    <el-pagination
      background
      @current-change="handleCurrentChange"
      :current-page="currentPageNum"
      :page-size="pageSize"
      layout="slot,prev"
      :total="total"
      style="padding-right:0"
    >
      <el-button
        :disabled="firstPageBtnDisabled"
        class="first-pager"
        @click="toFirstPage"
      >{{$t('page.first')}}</el-button>
    </el-pagination>
    <el-pagination
      background
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :current-page="currentPageNum"
      :page-size="pageSize"
      :page-sizes="[10,20,30]"
      layout="pager,next,slot,total,sizes,jumper"
      :total="total"
      :pager-count="5"
      style="padding-left:0"
    >
      <el-button
        :disabled="lastPageBtnDisabled"
        class="last-pager"
        @click="toLastPage"
      >{{$t('page.last')}}</el-button>
    </el-pagination>
    <div class="confirm">{{$t('page.confirm')}}</div>
  </div>
</template>
<script>
export default {
  name: "pages",
  data() {
    return {
      currentPageNum: this.currentPage,
      firstPageBtnDisabled: true,
      lastPageBtnDisabled: false,
      langChange: true
    };
  },
  props: {
    currentPage: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 10
    },
    total: {
      type: Number,
      default: 0
    }
  },
  watch: {
    currentPage(newVal) {
      this.currentPageNum = newVal;
      this.firstPageBtnDisabled = this.currentPageNum === 1 ? true : false;
      this.lastPageBtnDisabled =
        this.currentPageNum === this.lastPageNum ? true : false;
    },
    "$store.state.initLang": {
      handler(lang) {
        this.langChange = false;
        setTimeout(() => {
          this.langChange = true;
        }, 100);
      },
      deep: true
    }
  },
  computed: {
    lastPageNum() {
      return Math.ceil(this.total / this.pageSize);
    }
  },
  methods: {
    handleCurrentChange(page) {
      this.firstPageBtnDisabled = page === 1 ? true : false;
      this.lastPageBtnDisabled = page === this.lastPageNum ? true : false;
      this.currentPageNum = page;
      this.$emit("pageChange", page);
    },
    handleSizeChange(size) {
      this.$emit("sizeChange", size);
    },
    toFirstPage(val) {
      this.handleCurrentChange(1);
    },
    toLastPage(val) {
      this.handleCurrentChange(this.lastPageNum);
    }
  }
};
</script>
<style lang="scss">
.page-container {
  .el-pagination {
    float: left;
    .el-pager li:not(.disabled).active {
      background-color: $blue;
    }
    .el-pager li {
      font-weight: 500;
      font-size: 12px;
    }
    .el-select .el-input {
      width: 110px;
    }
    .el-pagination__sizes {
      margin-right: 0;
    }
  }
  .confirm {
    padding: 0 15px;
    margin-left: 13px;
    display: inline-block;
    width: fit-content;
    height: 30px;
    line-height: 30px;
    background: $blue;
    border-radius: 15px;
    text-align: center;
    color: $white;
    font-size: 14px;
    font-family: PingFangSC-Regular;
    cursor: pointer;
  }
  .el-pagination__jump {
    margin-left: 5px;
  }
  // .el-pager .number,
  .first-pager,
  .last-pager {
    background: #f4f4f5;
  }
  .last-pager {
    margin-right: 10px;
  }
}
</style>